# -*- coding:utf-8 *-
# 项目       :    all_auto 
# 文件       :    my_page.py
# 作者       :    zhangchen 
# 时间       :    2021/3/29  2:03 下午 
# IDE       :    PyCharm

from public.App_Page import AppPage, BasePage
from lib.loggers import log
import time
from lib.func import think_time


class My(BasePage):


    @think_time
    def go_mypage(self):
        self.find_text("养车").click()
        while not self.find_text("养车"):
            # self.back()
            if self.find_text("养车"):
                self.back()
            time.sleep(0.5)





    @think_time
    def textclick(self, text):
        '''
        text 点击
        :param text: 文本
        :return:
        '''
        self.find_text(text).click()

    @think_time
    def get_window(self,text):
        return self.gettitle_text(self.find_text(text))


    @think_time
    def get_title(self, id):
        return self.gettitle_text(self.find_id(id))

    @think_time
    def get_window_id(self, id):
        # 根据文本查找文本
        return self.find_xpath(self.find_id(id))

    @think_time
    def get_Xpath(self, text):
        return self.gettitle_xpath(self.find_text(text))

    @think_time
    def get_id(self, id):
        return self.gettitle_id(self.find_id(id))


    @think_time
    def idclick(self, id):
        '''
        resourceId 点击
        :param id:
        :return:
        '''
        self.find_id(id).click()

    @think_time
    def clssclik(self,classname):
        '''
        classname点击
        :param classname:
        :return:
        '''
        self.find_class(classname).click()

    def xpath(self,el):
        """
        xpath 点击
        :param el: xpath
        :return:
        """
        self.find_xpath(el).click()

    def huaDongChaZhao(self):
        '''滑动整屏'''
        self.d(scrollable=True).fling()




    @think_time
    def scrollable_description(self, position):
        '''
        根据文本滑动找到某个特定元素
        :param position:
        :return:
        '''
        self.d(scrollable=True).scroll.to(text=position)

    @think_time
    def scrollable_description1(self):
        '''
        根据文本滑动找到某个特定元素
        :param position:
        :return:
        '''
        self.d(scrollable=True).scroll.to(resourceId='com.truckhome.bbs:id/iv_info_single_title')
        # 向上滑动

    @think_time
    def scrollable_up(self):
        try:
            self.d.swipe_ext("up")
            time.sleep(1)
        except:
            raise
        finally:
            time.sleep(1)

    #
    # def san(self, id):
    #     """
    #     判断元素是否存在
    #     :param xpath: xpath路径
    #     :return:
    #     """
    #     self.d(resourceId=id).click()

    @think_time
    def login_yc(self, user, pwd):
        if self.find_text('微信一键登录'):
            self.d(text='使用手机号码登录').click()
            self.d(text='账号密码登录').click()
            self.d(resourceId='com.truckhome.bbs:id/iv_login_agree').click()
            self.input_login(user, 'com.truckhome.bbs:id/et_login_account')
            self.input_login(pwd, 'com.truckhome.bbs:id/et_login_pwd')
            self.d(resourceId='com.truckhome.bbs:id/iv_login_pwd').click()
        else:
            return

    @think_time
    def wait_findele(self, ele):
        while True:
            if self.d(resourceId=ele).wait(timeout=1):
                break
            self.d.swipe_ext("up")

    @think_time
    def get_toast(self):
        # 捕获toast提示
        t = self.d.toast.get_message()
        return t

    @think_time
    def wait_youhua(self, ele):
        while True:
            if self.d(resourceId=ele).wait(timeout=1):
                break
            self.d.swipe(0.887, 0.138, 0.078, 0.143)

    @think_time
    def xpathclick(self, xpath):
        return self.d.xpath(xpath).click()


    def input_KM(self,text):
        #输入
        self.input_text(self.d.xpath('//*[@resource-id="app"]/android.view.View[1]/android.widget.EditText[1]'), text)

    def input_DL(self,text):
        #输入
        self.input_text(self.d.xpath('//*[@resource-id="app"]/android.view.View[1]/android.widget.ListView[2]/android.view.View[1]/android.widget.EditText[1]'), text)

    def input_XJB(self,text):
        #输入
        self.input_text(self.d.xpath('//*[@resource-id="app"]/android.view.View[1]/android.widget.ListView[2]/android.view.View[2]/android.widget.EditText[1]'), text)

    def input_AQX(self,text):
        #输入
        self.input_text(self.d.xpath('//*[@resource-id="app"]/android.view.View[1]/android.widget.ListView[1]/android.view.View[3]/android.widget.EditText[1]'), text)

    def input_CZNL(self,text):
        #输入
        self.input_text(self.d.xpath('//*[@resource-id="app"]/android.view.View[1]/android.widget.ListView[1]/android.view.View[4]/android.widget.EditText[1]'), text)

    def input_WDPJL(self,text):
        #输入
        self.input_text(self.d.xpath('//*[@resource-id="app"]/android.view.View[1]/android.widget.EditText[1]'), text)

    def input_WDXQ(self,text):
        #输入
        self.input_text(self.d.xpath('//*[@resource-id="com.truckhome.bbs:id/et_comment"]'), text)

    @think_time
    def wait_findelexp(self, xpath):
        while True:
            if self.d.xpath(xpath).wait(timeout=1):
                break
            self.d.swipe_ext("up")

    @think_time
    def wait_findeletextdown(self, ele):
        while True:
            if self.d(text=ele).wait(timeout=1):
                break
            self.d.swipe_ext("down")

    @think_time
    def wait_findeletextup(self, ele):
        while True:
            if self.d(text=ele).wait(timeout=1):
                break
            self.d.swipe_ext("up")

    @think_time
    def exit_one(self):
        self.find_text("我的").click()
        while not self.find_text("我的"):
            # self.back()
            if self.find_text("我的"):
                # self.back()
                time.sleep(0.5)

    def exit_second(self):
        if self.find_id('com.truckhome.bbs:id/iv_toolbar_setting'):
            self.d(resourceId='com.truckhome.bbs:id/iv_toolbar_setting').click()
            self.d(text='退出登录').click()
            self.d(text='确定').click()

    def notification(self):
        """
        打开通知消息
        :return:
        """
        self.d.notification()
